public class Solution {
    public int longestConsecutive(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int num : nums) {
            map.putIfAbsent(num, 0);
        }
        int maxLen = 0;
        for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if(entry.getValue() == 1) {
                continue;
            }

            int len = 1;
            entry.setValue(1);
            for(int i = entry.getKey() + 1; map.containsKey(i); i++) {
                map.put(i, 1);
                len++;
            }
            for(int i = entry.getKey() - 1; map.containsKey(i); i--) {
                map.put(i, 1);
                len++;
            }
            maxLen = Math.max(maxLen, len);
        }
        return maxLen;
    }
}
